package com.jsjosem.aftersales.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.data.response.NotificationsResponse;
import com.jsjosem.aftersales.data.response.WorkTicketNotify;

import java.util.List;
import java.util.Objects;

public class WorkNotifyAdapter extends RecyclerView.Adapter<WorkNotifyAdapter.NotifyWorkTicketViewHolder> {

    private final Context mContext;

    private final List<NotificationsResponse> mEventNotifies;

    private OnItemClickListener mOnItemClickListener;
    private View view;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public WorkNotifyAdapter(Context mContext, List<NotificationsResponse> mEventNotifies) {
        this.mContext = mContext;
        this.mEventNotifies = mEventNotifies;
    }

    @NonNull
    @Override
    public NotifyWorkTicketViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.notify_work_ticket_item_list, parent, false);
        NotifyWorkTicketViewHolder notifyWorkTicketViewHolder = new NotifyWorkTicketViewHolder(view);
        view.setOnClickListener(v -> {
            if (Objects.nonNull(mOnItemClickListener)) {
                mOnItemClickListener.onItemClick(v, notifyWorkTicketViewHolder.getAdapterPosition());
            }
        });
        return notifyWorkTicketViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull NotifyWorkTicketViewHolder holder, int position) {
        holder.tvWorkNotifyTime.setText(mEventNotifies.get(position).getmCreateTimeFormat());
        holder.tvWorkNotifyTitle.setText(mEventNotifies.get(position).getmTitle());
        // 已读，隐藏红点控件
        if (mEventNotifies.get(position).getmStatus()==1) {
            holder.ivUnread.setVisibility(View.GONE);
        }
        holder.tvWorkHandlersValue.setText(mEventNotifies.get(position).getmUserName());
        holder.tvCustomerNameValue.setText(mEventNotifies.get(position).getmCustomerName());
        holder.tvContactValue.setText(mEventNotifies.get(position).getmContactName());
        holder.tvPhoneValue.setText(mEventNotifies.get(position).getmContactPhone());
        holder.tvAddressValue.setText(mEventNotifies.get(position).getmAddress());
        holder.tvScheduleTimeValue.setText(mEventNotifies.get(position).getmPlanAt());
    }

    @Override
    public int getItemCount() {
        return mEventNotifies.size();
    }

    static class NotifyWorkTicketViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvWorkNotifyTime;

        private final TextView tvWorkNotifyTitle;

        private final ImageView ivUnread;

        private final TextView tvWorkHandlersValue;

        private final TextView tvCustomerNameValue;

        private final TextView tvContactValue;

        private final TextView tvPhoneValue;

        private final TextView tvAddressValue;

        private final TextView tvScheduleTimeValue;

        public NotifyWorkTicketViewHolder(@NonNull View itemView) {
            super(itemView);
            tvWorkNotifyTime = itemView.findViewById(R.id.tv_work_notify_time);
            tvWorkNotifyTitle = itemView.findViewById(R.id.tv_work_notify_title);
            ivUnread = itemView.findViewById(R.id.iv_unread);
            tvWorkHandlersValue = itemView.findViewById(R.id.tv_work_handlers_value);
            tvCustomerNameValue = itemView.findViewById(R.id.tv_customer_name_value);
            tvContactValue = itemView.findViewById(R.id.tv_contact_value);
            tvPhoneValue = itemView.findViewById(R.id.tv_phone_value);
            tvAddressValue = itemView.findViewById(R.id.tv_address_value);
            tvScheduleTimeValue = itemView.findViewById(R.id.tv_schedule_time_value);
        }
    }
}
